package com.yakuj3.android.comptadroid.adapter;

import android.content.Context;
import android.database.Cursor;
import android.graphics.Color;
import android.support.v4.widget.CursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.yakuj3.android.comptadroid.R;

public class MyOperationCursorAdapter extends CursorAdapter {

	public MyOperationCursorAdapter(Context context, Cursor c) {
		super(context, c, false);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void bindView(View view, Context context, Cursor cursor) {
		// TODO Auto-generated method stub
		
		if (cursor != null)
		{
			TextView text1 = (TextView)view.findViewById(R.id.operation_description_item);
			TextView text2 = (TextView)view.findViewById(R.id.operation_amount_item);
			
			String sMontant = cursor.getString(cursor.getColumnIndex(cursor.getColumnName(4)));
			double dMontant = Math.round(Float.parseFloat(sMontant) * 100.0) / 100.0;
			
			text1.setText(cursor.getString(cursor.getColumnIndex(cursor.getColumnName(3))));
			
			if (dMontant >= 0)
			{
				text2.setText("+ " + dMontant + " euros");
				text2.setTextColor(Color.parseColor("#008000"));
			}
			else
			{
				text2.setText("- " + (-dMontant) + " euros");
				text2.setTextColor(Color.parseColor("#FF0000"));
			}
		}
	}

	@Override
	public View newView(Context context, Cursor cursor, ViewGroup parent) {
		// TODO Auto-generated method stub
		
		LayoutInflater inflater = LayoutInflater.from(parent.getContext());
		View rowView = inflater.inflate(R.layout.operation_listview_item, parent, false);
		
		return rowView;
	}

}
